package com.farm.portal.dao.impl;

import java.math.BigInteger;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.farm.portal.domain.Component;
import com.farm.portal.domain.Componentdef;
import com.farm.portal.domain.Portal;
import com.farm.portal.domain.Portaltree;
import com.farm.portal.dao.PortalDaoInter;
import com.farm.core.sql.query.DBRule;
import com.farm.core.sql.query.DBSort;
import com.farm.core.sql.query.DataQuery;
import com.farm.core.sql.result.DataResult;
import com.farm.core.sql.utils.HibernateSQLTools;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/* *
 *功能：门户定义持久层实现
 *详细：
 *
 *版本：v0.1
 *作者：FarmCode代码工程
 *日期：20141204174206
 *说明：
 */
public class PortalDaoImpl implements PortalDaoInter {
	private SessionFactory sessionFatory;
	private HibernateSQLTools<Portal> sqlTools;

	@Override
	public void deleteEntity(Portal portal) {
		// TODO 自动生成代码,修改后请去除本注释
		Session session = sessionFatory.getCurrentSession();
		session.delete(portal);
	}

	@Override
	public int getAllListNum() {
		// TODO 自动生成代码,修改后请去除本注释
		Session session = sessionFatory.getCurrentSession();
		SQLQuery sqlquery = session
				.createSQLQuery("select count(*) from farm_code_field");
		BigInteger num = (BigInteger) sqlquery.list().get(0);
		return num.intValue();
	}

	@Override
	public Portal getEntity(String portalid) {
		// TODO 自动生成代码,修改后请去除本注释
		Session session = sessionFatory.getCurrentSession();
		return (Portal) session.get(Portal.class, portalid);
	}

	@Override
	public Portal insertEntity(Portal portal) {
		// TODO 自动生成代码,修改后请去除本注释
		Session session = sessionFatory.getCurrentSession();
		session.save(portal);
		return portal;
	}

	@Override
	public void editEntity(Portal portal) {
		// TODO 自动生成代码,修改后请去除本注释
		Session session = sessionFatory.getCurrentSession();
		session.update(portal);
	}

	@Override
	public Session getSession() {
		// TODO 自动生成代码,修改后请去除本注释
		return sessionFatory.getCurrentSession();
	}

	@Override
	public DataResult runSqlQuery(DataQuery query) {
		// TODO 自动生成代码,修改后请去除本注释
		try {
			return query.search(sessionFatory.getCurrentSession());
		} catch (Exception e) {
			return null;
		}
	}

	@Override
	public void deleteEntitys(List<DBRule> rules) {
		// TODO 自动生成代码,修改后请去除本注释
		sqlTools
				.deleteSqlFromFunction(sessionFatory.getCurrentSession(), rules);
	}

	@Override
	public List<Portal> selectEntitys(List<DBRule> rules) {
		// TODO 自动生成代码,修改后请去除本注释
		return sqlTools.selectSqlFromFunction(
				sessionFatory.getCurrentSession(), rules);
	}

	@Override
	public void updataEntitys(Map<String, Object> values, List<DBRule> rules) {
		// TODO 自动生成代码,修改后请去除本注释
		sqlTools.updataSqlFromFunction(sessionFatory.getCurrentSession(),
				values, rules);
	}

	@Override
	public int countEntitys(List<DBRule> rules) {
		// TODO 自动生成代码,修改后请去除本注释
		return sqlTools.countSqlFromFunction(sessionFatory.getCurrentSession(),
				rules);
	}

	public SessionFactory getSessionFatory() {
		return sessionFatory;
	}

	public void setSessionFatory(SessionFactory sessionFatory) {
		this.sessionFatory = sessionFatory;
	}

	public HibernateSQLTools<Portal> getSqlTools() {
		return sqlTools;
	}

	public void setSqlTools(HibernateSQLTools<Portal> sqlTools) {
		this.sqlTools = sqlTools;
	}

	@Override
	public Portal getEntityByKey(String key) {
		List<Portal> list = this.selectEntitys(DBRule.addRule(
				new ArrayList<DBRule>(), "KEYSTR", key, "="));
		if (list.size() > 0) {
			return list.get(0);
		} else {
			return null;
		}
	}

	@Override
	public List<Componentdef> getUserPortalComponentDefs(String key,
			String userId) {
		List<Componentdef> list = new ArrayList<Componentdef>();
		DataQuery query = DataQuery
				.getInstance(
						1,
						"c.NAME,c.SORT,e.ICONCLASS,e.URL,d.id,d.CLOSABLE,d.COLLAPSIBLE,d.HEITGH,d.OPENABLE,d.WIDTH,g.CLOSEIS",
						"FARM_PORTAL_DEFINE a LEFT JOIN FARM_PORTAL_TREE b ON b.PORTALDEFID=a.ID LEFT JOIN FARM_PORTAL_TREE c ON c.PARENTID=b.ID LEFT JOIN FARM_PORTAL_COMPONENTDEF d ON d.ID=c.COMPONENTDEFID LEFT JOIN FARM_PORTAL_COMPONENT e ON e.ID=d.COMPONENTID LEFT JOIN FARM_PORTAL_DEFINEEXCUTE f ON f.PORTALDEFID=a.id AND f.CUSER='"
								+ userId
								+ "' LEFT JOIN FARM_PORTAL_COMPONENTEXCUTE g ON g.PORTALEXCUTEID=f.ID AND g.COMPONENTDEFID=d.ID");
		query.addRule(new DBRule("a.KEYSTR", key, "="));
		query.addRule(new DBRule("e.PSTATE", "1", "="));
		query.setPagesize(20);
		query.addSort(new DBSort("b.SORT", "asc"));
		DataResult result = null;
		try {
			result = query.search();
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
		for (Map<String, Object> node : result.getResultList()) {
			Portaltree tree = new Portaltree();
			tree.setName(node.get("C_NAME").toString());
			tree.setSort(Integer.valueOf(node.get("C_SORT").toString()));
			Component component = new Component();
			component.setUrl(node.get("E_URL").toString());
			component.setIconclass(node.get("E_ICONCLASS").toString());
			Componentdef componentdef = new Componentdef();
			componentdef.setNode(tree);
			componentdef.setId(node.get("D_ID").toString());
			componentdef.setClosable(node.get("D_CLOSABLE").toString());
			componentdef.setCollapsible(node.get("D_COLLAPSIBLE").toString());
			componentdef.setComponent(component);
			componentdef.setHeitgh(Integer.valueOf(node.get("D_HEITGH")
					.toString()));
			componentdef.setOpenable(node.get("D_OPENABLE").toString());
			componentdef.setWidth(Integer.valueOf(node.get("D_WIDTH")
					.toString()));
			if (node.get("G_CLOSEIS") != null
					&& node.get("G_CLOSEIS").toString().trim().length() > 0) {
				componentdef.setOpenable(node.get("G_CLOSEIS").toString()
						.equals("1") ? "0" : "1");
			}
			list.add(componentdef);
		}
		return list;
	}

}
